<?php
class BoletasController extends AppController {

	var $name = 'Boletas';

	var $helpers = array('Html', 'Form', 'Xls');

	var $uses = array('Boleta','Materia');
	
	function index($field = 'nombre', $key = null) {
		
		$c = '';
		
		if(!empty($key))
		{
			$c = array(
				'Boleta.'. $field .' LIKE' => '%'.$key.'%'
			);
		}
		
		$this->paginate = array(
			'contain' => array(
				'Area' => array(
					'fields'=> array(
						'materia_id'
					)
				)
			),
			'conditions' => $c
		);
		
		$boletas = $this->paginate('Boleta');
				
		$this->set(compact('boletas'));	

	}
	
	function xls(){
		
		$params = array(
			'recursive' => 1
		);//$this->Boleta->query('SET NAMES latin1');
		
		$boletas = $this->Boleta->find('all', $params);		

		$boletasxls = array();
		
		foreach($boletas as $b){
			
			$tmp = array();
			
			$tmp['cedula'] = 'id-' . $b['Boleta']['cedula'];
			$tmp['nombre'] = $b['Boleta']['nombre'];
			$tmp['apellido1'] = $b['Boleta']['apellido1'];
			$tmp['apellido2'] = $b['Boleta']['apellido2'];
			$tmp['telefono1'] = $b['Boleta']['telefono1'];
			$tmp['telefono2'] = $b['Boleta']['telefono2'];
			$tmp['telefono3'] = $b['Boleta']['telefono3'];
			$tmp['correo'] = $b['Boleta']['correo'];
			$tmp['domicilio'] = $b['Boleta']['domicilio'];
			$tmp['trabaja'] = $b['Boleta']['trabaja'];
			$tmp['lugartrabajo'] = $b['Boleta']['lugartrabajo'];
			$tmp['empresatrabajo'] = $b['Boleta']['empresatrabajo'];
			$tmp['procedencia'] = $b['Boleta']['procedencia'];
			$tmp['egreso'] = $b['Boleta']['egreso'];
			$tmp['adecuacion'] = $b['Boleta']['adecuacion'];
			$tmp['hora'] = $b['Boleta']['hora'];
			$tmp['modo'] = $b['Modo']['nombre'];
			$tmp['provincia'] = $b['Provincia']['nombre'];
	        $tmp['canton'] = $b['Cantone']['nombre'];
			$tmp['distrito'] = $b['Distrito']['nombre'];
			
			$boletasxls[] = array_values($tmp);			
		}
		
		$boletas = $boletasxls;
		
		$this->set(compact('boletas'));

		$this->layout = 'xls';
	}
	
	function reportes(){
		
		$materias = $this->Materia->find('list', array('fields'=> array('nombre')));
		
		$this->set(compact('materias'));
		
	}
	
	function pormateria($materiaId = 0){
		
		$params = array(
			/*'contain' => array(
				'Boleta.nombre',
				'Boleta.cedula',
				'Area.cantidad',
				'Materia.nombre',
				'Materia',
				'Boleta' => array(
					'Cantone.nombre',
					'Distrito.nombre',
					'Modo.nombre'				
				)
			),*/
			'recursive' => 1
		);
		
		$this->Boleta->query('SET NAMES latin1');
		
		$boletas = $this->Boleta->find('all', $params);
			
		//debug($boletas);
		
		$boletasxls = array();
		
		foreach($boletas as $b){
			
			$tmp = array();
			
			/*$tmp = array_values($b['Boleta']);
			
			$tmp = am($tmp, $b['Modo']);
			
			$tmp = am($tmp, $b['Provincia']);
			
			$tmp = am($tmp, $b['Cantone']);
			
			$tmp = am($tmp, $b['Distrito']);*/
			
			$tmp['cedula'] = $b['Boleta']['cedula'];
			$tmp['nombre'] = $b['Boleta']['nombre'];
			$tmp['apellido1'] = $b['Boleta']['apellido1'];
			$tmp['apellido2'] = $b['Boleta']['apellido2'];
			$tmp['telefono1'] = $b['Boleta']['telefono1'];
			$tmp['telefono2'] = $b['Boleta']['telefono2'];
			$tmp['telefono3'] = $b['Boleta']['telefono3'];
			$tmp['correo'] = $b['Boleta']['correo'];
			$tmp['domicilio'] = $b['Boleta']['domicilio'];
			$tmp['trabaja'] = $b['Boleta']['trabaja'];
			$tmp['lugartrabajo'] = $b['Boleta']['lugartrabajo'];
			$tmp['empresatrabajo'] = $b['Boleta']['empresatrabajo'];
			$tmp['procedencia'] = $b['Boleta']['procedencia'];
			$tmp['egreso'] = $b['Boleta']['egreso'];
			$tmp['adecuacion'] = $b['Boleta']['adecuacion'];
			$tmp['hora'] = $b['Boleta']['hora'];
			$tmp['modo'] = $b['Modo']['nombre'];
			$tmp['provincia'] = $b['Provincia']['nombre'];
	        $tmp['canton'] = $b['Cantone']['nombre'];
			$tmp['distrito'] = $b['Distrito']['nombre'];
			
			$boletasxls[] = $tmp;			
		}		
		
		$this->set(compact('boletasxls'));

		$this->layout = 'xls';		
	}
		
	function buscar()
	{	
		if(!empty($this->data))
		{

			$this->redirect('/boletas/index/' . $this->data['Boleta']['field'] . '/' . $this->data['Boleta']['key']);			

		}				
	}
	
	function view($id = null) {

		if (!$id) {

			$this->Session->setFlash('Example message text', 'default', array('class' => 'warning'));


			$this->redirect(array('action'=>'index'));

		}
		$params = array(
			'conditions' => array(
				'Boleta.id' => $id
			),
			'recursive' => 2
		);
		
		$boleta = $this->Boleta->find('first', $params);
		
		$this->set(compact('boleta'));

	}

	function add() {

		if (!empty($this->data)) {
		
			foreach($this->data['Area'] as $k=>$a){
				if($a['checked'] == 0){
					unset($this->data['Area'][$k]);
				}
			}
			$this->data['Area'] = array_values($this->data['Area']);

			$this->Boleta->create();			

			if(empty($this->data['Dia']['Dia']))
			{
				unset($this->data['Dia']);
			}

			if(empty($this->data['Area']))
			{
				unset($this->data['Area']);
			}
//debug($this->data);
			if ($this->Boleta->saveAll($this->data)) {

				$this->Session->setFlash(__('The Boleta has been saved', true),  'default', array('class' => 'success'));

				$this->redirect(array('action'=>'index'));

			} else {
				$this->Session->setFlash(__('The Boleta could not be saved. Please, try again.', true),  'default', array('class' => 'warning'));
			}
			
		}
		$modos = $this->Boleta->Modo->find('list', array('fields'=> array('nombre')));
		$provincias = $this->Boleta->Provincia->find('list', array('fields'=> array('nombre')));
		$cantones = $this->Boleta->Cantone->find('list', array('fields'=> array('nombre')));
		$distritos = $this->Boleta->Distrito->find('list', array('fields'=> array('nombre')));
		$materias = $this->Materia->find('list', array('fields'=> array('nombre')));
		$dias = $this->Boleta->Dia->find('list', array('fields'=> array('nombre')));		
		$horas = array('M' => __('MANANA',true),'T' =>  __('TARDE',true),'N' =>  __('NOCHE',true));
		
		$this->set(compact('modos', 'provincias', 'cantones', 'distritos', 'materias', 'dias', 'horas'));
	}

	function edit($id = null) {
		
		if (!$id && empty($this->data)) {

			$this->Session->setFlash(__('Invalid Boleta', true));

			$this->redirect(array('action'=>'index'));

		}
		if (!empty($this->data)) {

			/*
			 * Actualizar las areas en que aplica una solicitud
			 * o eliminar si unchecked
			 */

//debug($this->data['Area']);

			$newAreas = array();
			
			foreach($this->data['Area'] as $k=>$a){
				
				//las areas existentes que se desmarcaron
				
				if(!empty($a['id']) && ($a['checked']==0)){
				
					$this->Boleta->Area->del($a['id']);
					
					unset($this->data['Area'][$k]);

				//quitar todos los un-checked
					
				}else if(empty($a['checked']) && empty($a['id'])){
					
					unset($this->data['Area'][$k]);
					
				}
			}
			
			$this->data['Area'] = array_values($this->data['Area']);
			
//debug($this->data['Area']);

			if(empty($this->data['Dia']['Dia']))
			{
				unset($this->data['Dia']);
			}

			if(empty($this->data['Area']))
			{
				unset($this->data['Area']);
			}
					
			if ($this->Boleta->saveAll($this->data)) {

				$this->Session->setFlash(__('The Boleta has been saved', true), 'default', array('class' => 'success'));

				$this->redirect(array('action'=>'index'));

			} else {

				$this->Session->setFlash(__('The Boleta could not be saved. Please, try again.', true), null, array('class' => 'error'));

			}
		}
		if (empty($this->data)) {

			$this->data = $this->Boleta->read(null, $id);

		}		
		/*
		 * Re establecer los checked Materias
		 * inyectados en data
		 */
		$tmp = array();

		foreach($this->data['Area'] as $a)
		{
			$a['checked'] = 1;
			$tmp[$a['materia_id']] = $a;
		}
		$this->data['Area'] = $tmp;
		/*
		 * Cargar los seleccionadores de Canton y distrito
		 * el valor existente y fue inyectado con ]read[
		 */
		//$this->data['Provincias'] = $this->Boleta->Provincia->find('list', array('fields'=> array('provincia')));
		$cantones = $this->Boleta->Cantone->find('list', array('fields'=> array('nombre')));
		$distritos = $this->Boleta->Distrito->find('list', array('fields'=> array('nombre')));
				
		$modos = $this->Boleta->Modo->find('list', array('fields'=> array('nombre')));
		$dias = $this->Boleta->Dia->find('list', array('fields'=> array('nombre')));
		$materias = $this->Materia->findAll();
		$horas = array(__('MANANA',true), __('TARDE',true), __('NOCHE',true));
		
		$this->set(compact('modos','cantones','distritos', 'materias', 'dias', 'horas'));
	}

	/*function editAreas($areas_nuevas, $id)
	{
debug($areas_nuevas);
		$area = $this->Boleta->Area->find('all',array('conditions'=>array('boleta_id'=>$id),'recursive'=>-1));
debug($area);

die;
		
	}*/
		
	function delete($id = null) {

		if (!$id) {
			
			$this->Session->setFlash(__('Invalid id for Boleta', true), null, array('class' => 'warning'));
			
			$this->redirect(array('action'=>'index'));
		
		}
		if ($this->Boleta->del($id, true)) {
		
			$this->Session->setFlash(__('Boleta deleted', true), null, array('class' => 'error'));
			
			$this->redirect(array('action'=>'index'));
		
		}
	}

}
?>